package com.ayoka.ui.training;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ayoka.Constant;
import com.ayoka.R;
import com.ayoka.adapter.TrainingGroupSectionAdapter;
import com.ayoka.base.BaseRecyclerViewActivity;
import com.ayoka.bean.TrainingBean;
import com.ayoka.bean.UserInformation;
import com.ayoka.databinding.ActivityTrainingCompletedBinding;
import com.ayoka.databinding.HeaderTrainingCompletedBinding;
import com.ayoka.mvp.TrainingGroupMVP.ITrainingGroupView;
import com.ayoka.mvp.TrainingGroupMVP.TrainingGroupModel.TrainingGroupBean;
import com.ayoka.mvp.TrainingGroupMVP.TrainingGroupModel.TrainingGroupSectionEntity;
import com.ayoka.mvp.TrainingGroupMVP.TrainingGroupPresenter;
import com.ayoka.ui.member.OpenMemberActivity;
import com.ayoka.utils.MemberEvent;
import com.jcp.baselibrary.brvah.BaseQuickAdapter;
import com.jcp.baselibrary.brvah.BaseViewHolder;
import com.jcp.baselibrary.model.ToolBarOptions;
import com.jcp.baselibrary.utils.UIUtils;

import java.util.List;
import java.util.Observable;


/**
 * @author jcp
 * @ClassName: TopicListActivity
 * @Description:
 * @date 2018-12-28 下午4:21:18
 */
public class TrainingCompletedActivity extends BaseRecyclerViewActivity<TrainingGroupSectionEntity, ActivityTrainingCompletedBinding, TrainingGroupPresenter> implements ITrainingGroupView {

    public static void start(Activity activity, long packageId) {
        Intent intent = new Intent(activity, TrainingCompletedActivity.class);
        intent.putExtra("PackageId", packageId);
        activity.startActivity(intent);
    }

    private long packageId;

    private HeaderTrainingCompletedBinding completedBinding;
    @Override
    protected void configIntentValue() {
        packageId = getIntent().getLongExtra("PackageId", 0);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_training_completed;
    }

    @Override
    protected void initViews() {
        super.initViews();
        completedBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.header_training_completed, null, false);
        completedBinding.btnComplete.setOnClickListener(this);
        adapter.addHeaderView(completedBinding.getRoot());
        adapter.setHeaderAndEmpty(true);
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return null;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        mDataBinding.recyclerView.setBackgroundColor(UIUtils.getColor(R.color.white_f2));
        return mDataBinding.recyclerView;
    }

    @Override
    protected BaseQuickAdapter<TrainingGroupSectionEntity, ? extends BaseViewHolder> getAdapter() {
        TrainingGroupSectionAdapter adapter = new TrainingGroupSectionAdapter(Constant.TrainingType.ADVANCED);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TrainingGroupSectionEntity item = (TrainingGroupSectionEntity) adapter.getItem(position);
                if (item.isHeader) return;
                TrainingBean trainingBean = item.t;
                if (trainingBean != null){
                    if (trainingBean.trainingType == Constant.TrainingType.ADVANCED){
                        if (trainingBean.IsAvailabe == 0){
                            mPresenter.showOpenMemberDialog(getSupportFragmentManager(), "进阶联系需要开通会员才能继续，请去开通会员");
                        } else {
                            if (trainingBean.CourseCompleted == 0) {
                                showToast(getString(R.string.please_complete_course_first));
                            } else {
                                TrainingPrepareActivity.start(TrainingCompletedActivity.this, trainingBean.ID, trainingBean.Title);
                            }
                        }
                    }
                }
            }
        });
        return adapter;
    }

    @Override
    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return null;
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
        mPresenter.getTrainingGroupData(isShowWaitDialog, 0, packageId);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions();
        options.navigateId = R.mipmap.icon_close_black_cross;
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        if (UserInformation.getInstance().user_info.IsVip == 0){
            MemberEvent.getInstance().addObserver(this);
        }
        getData(true);
    }

    @Override
    protected TrainingGroupPresenter getPresenter() {
        return new TrainingGroupPresenter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == completedBinding.btnComplete){
            finish();
        }
    }

    @Override
    public void configTrainingGroupData(TrainingGroupBean trainingGroupBean) {
        if (trainingGroupBean == null){
            setLoadMoreLayout(null);
        } else {
            List<TrainingGroupSectionEntity> result = mPresenter.formatAdvanceList(trainingGroupBean);
            setLoadMoreLayout(result);
        }
    }

    @Override
    public void onOpenMemberClick() {
        OpenMemberActivity.start(this, Constant.OpenMember.OPEN_MEMBER);
    }

    @Override
    public void update(Observable observable, Object data) {
        super.update(observable, data);
        if (observable instanceof MemberEvent){
            if (mPresenter != null){
                onRefresh();
            }
        }
    }

    @Override
    protected void onDestroy() {
        MemberEvent.getInstance().deleteObserver(this);
        super.onDestroy();
    }
}
